package cn.pengpeng.day01.hdfs;



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;

public class HdfsDemo {
	FileSystem fileSystem = null;
	@Before
	public void init() throws Exception{
		Configuration conf = new Configuration();
		//conf.set(name, value);
		//获取文件系统
		fileSystem = FileSystem.get(new URI("hdfs://bigdata01:9000/"),conf, "root");
	}
	
	//上传文件
	@Test
	public void testUpLoad() throws Exception{
		fileSystem.copyFromLocalFile(new Path("D:\\program\\apache\\spark-1.6.3-bin-hadoop2.6.tgz"), new Path("/bike/"));
		fileSystem.close();
	}
	
	//下载文件
	@Test
	public void testDownLoad() throws Exception{
		fileSystem.copyToLocalFile(new Path("/bikes.log"), new Path("D:\\bikes.log"));
		fileSystem.close();
	}
	
	//下载文件
	public void testDownLoad2(){
		
	}
	
	//删除文件
	@Test
	public void testDelet() throws Exception{
		fileSystem.delete(new Path("/bikes.log"), true);
		fileSystem.close();
	}
	
	//改名，移动
	@Test
	public void testReName() throws Exception{
		//需要先有父目录
		fileSystem.rename(new Path("/bikess.log"), new Path("/bike/bike.log"));
		fileSystem.close();
	}
	
	
	//创建目录
	@Test
	public void testMkdir() throws IllegalArgumentException, IOException{
		fileSystem.mkdirs(new Path("/mkdir/a/b/c"));
		fileSystem.close();
	}
	
	//查看目录信息
	@Test
	public void testListDir() throws Exception{
		RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/mkdir"), true);
		while(listFiles.hasNext()){
			LocatedFileStatus fileInfo = listFiles.next();
			//文件位置   ， 大小 ，文件块都存放在哪里，文件副本数。。。
			System.out.println(fileInfo.getPath());
			System.out.println(fileInfo.getLen());
			System.out.println(fileInfo.getBlockSize());
			System.out.println(fileInfo.getReplication());
			
			BlockLocation[] blockLocations = fileInfo.getBlockLocations();
			for(int i = 0;i<blockLocations.length;i++){
				BlockLocation blockLocation = blockLocations[i];
				System.out.println(blockLocation);
			}
			System.out.println("--------华丽丽的分割线------------------");
		}
	}
	
	//查看目录信息2
	@Test
	public void testListDir2() throws FileNotFoundException, IllegalArgumentException, IOException{
		FileStatus[] listStatus = fileSystem.listStatus(new Path("/bike"));
		for (int i = 0; i < listStatus.length; i++) {
			if(listStatus[i].isDirectory()){
				System.out.println("这个是一个目录");
			}
			if(listStatus[i].isFile()){
				System.out.println("这个是一个文件");
			}
			
			FileStatus fileInfo = listStatus[i];
			//文件位置   ， 大小 ，文件块都存放在哪里，文件副本数。。。
			System.out.println(fileInfo.getPath());
			System.out.println(fileInfo.getLen());
			System.out.println(fileInfo.getBlockSize());
			System.out.println(fileInfo.getReplication());
			//BlockLocation[] blockLocations = fileInfo.getBlockLocations();
			
			System.out.println("--------华丽丽的分割线------------------");
		}
		fileSystem.close();
	}
	
	//读取文件内容
	@Test
	public void readFile() throws IllegalArgumentException, IOException{
		FSDataInputStream open = fileSystem.open(new Path("/wirte.txt"));
		BufferedReader read = new BufferedReader(new InputStreamReader(open));
		String line = null;
		while((line = read.readLine())!=null){
			System.out.println(line);
		}
		open.close();
		fileSystem.close();
		
	}
	
	//写入文件内容
	@Test
	public void write() throws IllegalArgumentException, IOException{
		FSDataOutputStream create = fileSystem.create(new Path("/wirte.txt"));
		//FSDataOutputStream create = fileSystem.append(new Path("/wirte.txt"));
		
		
		create.write("nihao\n".getBytes());
		create.write("haha\n".getBytes());
		create.close();
		
		
	}
}
